package com.example.demo.huawei;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

/*
span>给定一个数组，编写一个函数来计算它的最大N个数与最小N个数的和。</span>你需要对数组进行去重。
<span><br> </span><br> <br> <span>说明：</span><br> <span>       *
数组中数字范围[0, 1000]</span><br> <span>       *
最大N个数与最小N个数不能有重叠，如有重叠，
输入非法返回-1</span><br> <span>*输入非法返回-1</span>
5
95 88 83 64 100
2
最大两数 100 95
最小两数 83 64
342
编程
 */
public class Main6 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = Integer.parseInt(scanner.nextLine());

        int[] heights = new int[n];
        String[] input = scanner.nextLine().split(" ");
        for (int i = 0; i < n; i++) {
            heights[i] = Integer.parseInt(input[i]);
        }

        int k = Integer.parseInt(scanner.nextLine());

        int process = process(heights, k);
        System.out.println(process);
    }

    /*
95 88 83 64 100

     */
    private static int process(int[] arr, int k) {
        if (k == 0) {
            return 0;
        }
        Arrays.sort(arr);
        Set<Integer> set = new LinkedHashSet<>();
        for (int i : arr) {
            if (i < 0 || i > 1000) {
                return -1;
            }
            set.add(i);
        }
        ArrayList<Integer> list = new ArrayList<>(set);
        //    64 83 88 95 100
        if (list.size() < 2 * k) {
            return -1;
        }
        int sum = 0;
//        for (int i = 0; i < k; i++) {
//            sum += list.get(i);
//        }
//        for (int i = list.size() - 1; i >= list.size() - k; i--) {
//            sum += list.get(i);
//        }

        int left = 0;
        int right = list.size() - 1;
        while (k > 0) {
            sum = sum + list.get(left) + list.get(right);
            left++;
            right--;
            k--;
        }

        return sum;
    }
}
